package com.cap.dreamcircle.View.Adapter.Item;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.cap.dreamcircle.Common.RecyclerView.BaseItem;
import com.cap.dreamcircle.Consts.Constants;
import com.cap.dreamcircle.Model.Action.RemoveFellowsForbiddensAction;
import com.cap.dreamcircle.Model.Bean.FellowFansForbiddenEntity;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.GlideUtils;
import com.cap.dreamcircle.View.PersonalInfoActivity;

import de.greenrobot.event.EventBus;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by jihongwen on 16/6/30.
 */

public class FellowFansForbiddensItem extends BaseItem<FellowFansForbiddenEntity> implements View.OnClickListener {

    FellowFansForbiddenEntity resultsBean;
    private CircleImageView imv_user_header;
    private TextView tv_user_name;
    private TextView tv_status;

    public FellowFansForbiddensItem(ViewGroup parent) {
        super(parent);
    }

    @Override
    public int getRes() {
        return R.layout.adapter_fellow_fans_forbidden;
    }

    @Override
    public void onCreateView(View rootView) {
        imv_user_header = (CircleImageView) rootView.findViewById(R.id.imv_user_header);
        tv_user_name = (TextView) rootView.findViewById(R.id.tv_user_name);
        tv_status = (TextView) rootView.findViewById(R.id.tv_status);
    }

    @Override
    public void onBindView(final FellowFansForbiddenEntity response) {
        this.resultsBean = response;
        GlideUtils.getInstance().LoadContextBitmap(getContext(), Constants.BASE_URL + resultsBean.getHeadImgUrl(), imv_user_header, R.mipmap.default_header, R.mipmap.default_header, GlideUtils.LOAD_BITMAP);
        tv_user_name.setText(resultsBean.getUserName());
        if (resultsBean.getUserType() == 1) {
            tv_status.setVisibility(View.INVISIBLE);
        } else {
            tv_status.setVisibility(View.VISIBLE);
            if(resultsBean.getUserType()==0){
                tv_status.setText("取消关注");
            }
            else{
                tv_status.setText("移除");
            }
            tv_status.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String title="";
                    if(resultsBean.getUserType()==0){
                        title="是否确定不再关注?";
                    }
                    else if(resultsBean.getUserType()==2){
                        title="是否确定移除黑名单?";
                    }
                    MaterialDialog dialog = new MaterialDialog.Builder(getContext()).title("提示").content(title).positiveText("确定").negativeText("取消").onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            RemoveFellowsForbiddensAction action=new RemoveFellowsForbiddensAction();
                            action.setUserId(resultsBean.getUserId());
                            action.setUserType(resultsBean.getUserType());
                            action.setPosition(getPosition());
                            EventBus.getDefault().post(action);
                        }
                    }).build();
                    dialog.show();
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        if (resultsBean.getUserType() != 2) {
            Intent intent =new Intent(getContext(), PersonalInfoActivity.class);
            intent.putExtra("userId",resultsBean.getUserId());
            ActivityCompat.startActivity((Activity) getContext(), intent, null);
        }
    }
}
